// pages/shop_info/shop_info.js
const db = wx.cloud.database();
const _ = db.command
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop:null,
    id:"",
    goods_number:0,
    love:0,
    array:[
     
    ],
    comment:""
  },
  change:function(){
    let that=this;
    try{
      db.collection('love').where({
        id: that.data.id,_openid:app.globalData.openid
      }).get({
        success: function (res) {
          // res.data 包含该记录的数据
          console.log(res.data);
          
          //获取到了物品的指定信息，
          //需要更新数目
          if (res.data.length==0){
            //没有就创建
            db.collection('love').add({
              data: {
                id: that.data.id,   //商品id
                number: 1
              },
              success:function(e){
                console.log(e);
              }
            })
          }else{
            //有的话就修改
            let real_id = res.data[0]._id
            let number = res.data[0].number;
            if(number==1){
              //1 喜欢
              db.collection('love').doc(real_id).update({
                data: {
                  number:0
                },
                success: function (e) {
                  console.log(e);
                }
              })
            }else{
              //0 不喜欢
              db.collection('love').doc(real_id).update({
                data: {
                  number: 1
                },
                success: function (e) {
                  console.log(e);
                }
              })
            }
          }
        }
      })
      
    }catch{
      console.log("出事了");
    }
    if(that.data.love==0){
      this.setData({
        love:1
      })
    }else{
      this.setData({
        love:0
      })
    }
  },
  input:function(event){
    // console.log(event.detail)
    this.setData({
      comment:event.detail.value
    })
    // console.log(this.data.comment)
  },
  submit:function(){
    //提交评论
    let that=this;
    db.collection('comment').add({
      data: {
        id: that.data.id,   //商品id
        comment: that.data.comment
      },
      success:function(e){
        console.log(e);
        that.getComment()
      }
    })



  },

  getComment:function(){
    //获取评论
    console.log("获取评论")
    let that=this;
    try{
      db.collection('comment').where({
        id: that.data.id,
      }).get({
        success:function(res){
          console.log(res)
          that.setData({
            array:res.data
          })
        }
      })
       
    }catch{
      console.log("出事了");
    }
    

  },
  /**
   * 生命周期函数--监听页面加载
   */
  getLove:function(){
    var that=this;
    console.log(app.globalData.openid)
    try{
      db.collection('love').where({
        id: that.data.id,_openid:app.globalData.openid
      }).get({
        success:function(res){
          console.log(res.data)
          let number=res.data[0].number
          console.log("number:",number)
          if(number==1){
            that.setData({
              love:1
            })
          }else{
            that.setData({
              love:0
            })
          }
          console.log(that.data.love)
        }
      })
    }catch{
      console.log("出事了");
    }
    console.log(that.data.love)
  },
  onLoad: function (options) {
    let id=options.id;
    let that=this;
    console.log(options);
    this.setData({
      id:options.id
    })

    db.collection('users').doc(id).get({
        success: function (res) {
          console.log(res)
          that.setData({
           shop:res.data,
           id:id
          })
        }
      })
    //获得购物车数目
    db.collection('goods_num').where({
      _openid: app.globalData.openid
    }).get({
      success: function (res) {
        console.log(res)
        that.setData({
          goods_number: res.data[0].goods_number
        })
      }
    })

     // 获取点赞星系
     this.getLove();

     //获取评论
     this.getComment()

  },

 
  
  
})